package com.faxsun.core.service.impl;

import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;

import com.faxsun.core.persist.dao.LiveInteractionDao;
import com.faxsun.core.persist.entity.LiveInteractionPO;
import com.faxsun.core.service.LiveInteractionService;
import com.faxsun.pojos.LiveInteractionStats;

@Transactional
public class LiveInteractionServiceImpl implements LiveInteractionService {
	@Autowired
	LiveInteractionDao liveInteractionDao;
    
    private final static String CACHE = "liveInteractionServiceCache";

	@Override
	public void saveOrUpdateStats(LiveInteractionPO liveInteractionPO) {
		LiveInteractionPO entity = this.liveInteractionDao.find(
				liveInteractionPO.getLiveId(), liveInteractionPO.getUserId());
		if (entity == null)
			liveInteractionDao.save(liveInteractionPO);
		else {
			entity.setSessionId(liveInteractionPO.getSessionId());
			entity.setUpdateTime(Calendar.getInstance().getTime());
			liveInteractionDao.update(entity);
		}
	}

	@Override
	public LiveInteractionStats getLiveInteractionStats(long liveId) {

		LiveInteractionStats lis = new LiveInteractionStats();
		lis.setLive(liveId);
		lis.setNoc(this.liveInteractionDao.getLiveNoc(liveId, 3).longValue());
		lis.setNop(this.liveInteractionDao.getLiveNop(liveId).longValue());
		return lis;
	}

    @Override
    @Cacheable(value = CACHE, key = "'getAllLiveViewer'")
    public long getAllLiveViewer() {
        return liveInteractionDao.getAllLiveViewer().longValue();
    }

}
